package net.feling.mcloud.connection;

import java.net.ConnectException;


public class ReconnectionTread extends Thread {

    private ConnectService service;
    private int waiting = 0;

    public ReconnectionTread(ConnectService connectService) {
        this.service = connectService;
    }

    @Override
    public void run() {
        try {
            while (!service.isConnected()) {
                Thread.sleep(waiting * 1000L);
                service.connect();
                waiting++;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ConnectException e) {
            e.printStackTrace();
        }

    }

    private int waiting() {
        if (waiting > 20) {
            return 600;
        }
        if (waiting > 13) {
            return 300;
        }
        return waiting <= 7 ? 10 : 60;
    }
}
